<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="com.hnas.core.util.PropertyUtil"%>
<%@page import="com.hnas.core.dto.DataBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>电子卡渠道信息</title>
<script type="text/javascript">
</script>
</head>
<body>
	<!-- 标题 -->
	<div class="titleBar m_b_20">
		<div class="tit fl b_l_e74a5f">
			<h3 class="cn">电子卡渠道信息</h3>
			<span class="en">ecard channel information</span>
		</div>
		<div class="fr"></div>
	</div>
	<!-- end 标题 -->

	<form method="post" action="${pageContext.request.contextPath }/backend/ecard/channel/search" name="searchForm"  >
  	<input type="hidden" name="pageNo" value="${dto.pageNo}">
  	<input type="hidden" name="totalCount" value="${dto.totalCount}">
	<!-- 查询 -->
	<div class="box_03 p_20 m_b_20">
		<table class="tb_style02" width="100%">
	       <tr>
               	<th>渠道编号</th>
               	<td><input id="channelCodeInput" name="channelCode" value="${dto.channelCode}" style="width:200px;" class="input_style2" type="text" placeholder=""></td>
               	<th>渠道名称</th>
               	<td><input id="channelNameInput" name="channelName" value="${dto.channelName}" style="width:200px;" class="input_style2" type="text" placeholder=""></td>
               	<th>备注</th>
               	<td><input id="remarkInput" name="remark" value="${dto.remark}" style="width:200px;" class="input_style2" type="text" placeholder=""></td>
            </tr>
	        <tr>
	        	<th style="width:130px;">开始时间</th>
               	<td>
               		<input type="text" style="width:130px;" name="startDate" id="startDate" value="${dto.startDate}" style="width:100px;" class="input_style2"/>
                </td>
                <th style="width:130px;">结束时间</th>
               	<td>
               		<input type="text" style="width:130px;" name="endDate" id="endDate" value="${dto.endDate}" style="width:100px;" class="input_style2"/>
                </td>
                <th style="width:130px;">每页显示</th>
               	<td>
               		<input type="text" style="width:30px;" name="pageSize" id="pageSize" value="${dto.pageSize}" style="width:100px;" class="input_style2"/>
                </td>
          </tr>
          <tr>   
               	<td colspan="5">
               		<input style="width:110px;margin-left:70px;" class="btn_brown02 m_r_12" type="button" name="searchData" id="searchData" value="查  询">
                	<!-- <input style="width:110px;" class="btn_brown02 m_r_12" type="button" name="createData" id="createData" value="添 加"> -->
                	<input class="btn_brown02 m_r_12" type="button" name="createChannels" id="createChannels" value="生 成 渠 道 物 料">
                	<input class="btn_brown02 m_r_12" type="button" name="importAmtData" id="importAmtData" value="生 成 渠 道 二 维 码 ">
                	<!-- <input class="btn_brown02 m_r_12" type="button" name="importAmtData" id="importAmtData" value="导 入 渠 道 信 息">
                	<input class="btn_brown02 m_r_12" type="button" name="downloadTemplate" id="downloadTemplate" value="下 载 模 板"> -->
                </td>
	         </tr>
		</table>
	</div> 
	<!-- end 查询 -->
				
</form>

<div class="m_b_20">
	<table class="tb_style05" width="100%" style="table-layout:fixed;">
		<thead>
		<tr>
			<c:forEach items="${beans}" var="o">
			<c:if test="${o.display}"><th>${o.title}</th></c:if>
			</c:forEach>
			<th width="100">操作</th> 
		</tr>
		</thead>
		<tbody>
			<c:forEach items="${pg.list}" var="o">
			<tr>
				<c:forEach items="${beans}" var="o1">
					<c:if test="${o1.display}">
					<td style="word-wrap:break-word;"><%=String.valueOf(PropertyUtil.getValue(pageContext.getAttribute("o"),((DataBean)pageContext.getAttribute("o1")).getField())).replace("false","否").replace("true","是")%></td></c:if>
				</c:forEach>
				<td>
					<a href="#" class="c_3366cc btn editData" channel_id="${o.id }">编辑</a>
				</td>
			</tr>
			</c:forEach>
		</tbody>
	</table>
	<div class="searpage" align="right"><div>${pg.pageStr}</div></div><br/>
	<div class="editDataDiv" id="editDIV" title="${pageTitle}" style="display: none;" >
		<form action="#" id="editFm" method="post" name="editFm" class="editFm" ><!-- <input type="hidden" name="id" id="dealid" value=0> -->
			<table >
				<c:forEach items="${beans}" var="o">
					<c:choose>
						<c:when test="${o.hidden}">
							<input type="hidden" name="${o.field}" id="deal${o.fieldId}" value="0">
						</c:when>
						<c:otherwise>
							<tr>
								<td width="26%" align="right"> 
									<label>${o.title}：</label>  
								</td>
								<c:if test="${o.tagType == 0}">
									<td> 
										<input type="text" name="${o.field}" id="deal${o.fieldId}" style="width:${o.length gt 0?o.length:200}px" <c:if test="${o.maxlength>0}">maxlength="${o.maxlength}"</c:if> <c:if test="${true == o.readonly}">readonly="readonly"</c:if>/>
										<c:if test="${not empty o.hint}">（${o.hint}）</c:if>
										<c:if test="${empty o.hint && o.dataType=='boolean'}">（1--是；0--否）</c:if>
										<c:if test="${!o.nullable}"><font color="red">*</font></c:if>
									</td>
								</c:if>
								<c:if test="${o.tagType == 1}">
									<td>
										<textarea name="${o.field}" id="deal${o.fieldId}" rows="3" style="width:${o.length gt 0?o.length:200}px" <c:if test="${o.maxlength>0}">maxlength="${o.maxlength}"</c:if> <c:if test="${true == o.readonly}">readonly="readonly"</c:if>></textarea>
										<c:if test="${not empty o.hint}">（${o.hint}）</c:if>
										<c:if test="${empty o.hint && o.dataType=='boolean'}">（1--是；0--否）</c:if>
										<c:if test="${!o.nullable}"><font color="red">*</font></c:if>
									</td>
								</c:if>
							</tr>
						</c:otherwise>
					</c:choose>
				</c:forEach>
				<tr>
	      			<td colspan="2">
	          			<p class="validateTips_deal" align="center"></p>
	      			</td>
	    		</tr>
			</table>
		</form>
	</div>
	<!-- 文件导出start -->
	<div class="ImportDataDiv" id="OutportDataDiv" title="导出渠道信息" style="display: none;" >
		<form action="#" id="Outportfm" method="post"  name="Outportfm">
			<table >
				<tr>
	 				<td colspan="2"> 
	 					<p class="validateTips" align="center"></p>
	 				</td>
	  			</tr>
				<tr>
	 				<td width="25%"> 
	 					<label>请输入渠道数量：</label>  
	 				</td>
					<td> 
						<input type="input" name="channelNum" id="channelNumInput"/> 
					</td>
	  			</tr>
			</table>
		</form>
	</div>
	<!-- 文件导出end -->
	<div class="ImportDataDiv" id="ImportDataDiv" title="导入渠道信息生成二维码" style="display: none;" >
	<form action="#" id="Importfm" method="post" enctype="multipart/form-data"  name="Importfm">
		<table >
			<tr>
 				<td colspan="2"> 
 					<p class="validateTips" align="center"></p>
 				</td>
  			</tr>
			<tr>
 				<td width="25%"> 
 					<label>导入渠道数据：</label>  
 				</td>
				<td> 
					<input type="file" name="formFile" id="formFile"/> 
				</td>
  			</tr>
		</table>
	</form>
</div>
	
</div>

<script>
	function gotoJumpPage(){
		var jumpNum= $("#jumpNum").val();
		$("#search_page_size").val($("#id_page_size").val());
		gotoPage(jumpNum);
		return false;
	}
	$(function(){
		$("#searchData").click(function(){
			/** 校验输入页数是否非法  **/
			var pageSize=$("#pageSize").val();
			var reg = new RegExp("^[0-9]*$");
			if($.trim(pageSize)!=""){
				if(!reg.test($.trim(pageSize))){
					alert("每页显示数请输入非负整数");
	 				return false;
				}
			}
			if($.trim(pageSize)==""){
				alert("每页显示数不应许为空");
				return false;
			}
			doSubmit();
		});
		
	});
	
	$("#startDate").datepicker({
		dateFormat:'yy-mm-dd',
		changeYear: true,
		changeMonth: true,
		yearRange: "1900:2049" 
	});
	
	$("#endDate").datepicker({
		dateFormat:'yy-mm-dd',
		changeYear: true,
		changeMonth: true,
		yearRange: "1900:2049" 
	});	
	$("#createData").click(function(){
		doReset(document.editFm);
		$("#editDIV").dialog("open");
	});
	//编辑
	$(".editData").click(function(){
		var id = $(this).attr("channel_id");
		$.ajax({
			url:"${pageContext.request.contextPath}/backend/ecard/channel/show/"+id,
			type: 'POST',
			error: function(XMLHttpRequest, textStatus, errorThrown){
				 $.dialog.alert({title:"查看",content:"加载数据异常。"});
			},
			success: function(data){
				console.log(data);
				fillDIV(data);
			}
		});
		$("#editDIV").dialog("open");
	});
	
	function fillDIV(data){
		<c:forEach items="${beans}" var="o">
			$("#deal${o.filterField}").val(data.${o.field});
		</c:forEach>
		$("#dealid").val(data.id);
	}
	/* $("#downloadTemplate").click(function(){
		location.href = "${pageContext.request.contextPath }/templete/channels.xls";
	}); */
	$("#createChannels").click(function(){
		$("#OutportDataDiv").dialog("open");
	});
	$("#importAmtData").click(function(){
		$("#ImportDataDiv").dialog("open");
	});
	$("#OutportDataDiv").dialog({
		autoOpen : false,
		height : 200,
		width : 450,
		modal : true,
		buttons : {
			"提 交" : function() {
				var channelNum = $("#channelNumInput").val();
				$(this).dialog("close");
				window.open("${pageContext.request.contextPath}/backend/ecard/channel/batchCreateChannel/"+channelNum);
				//location.href = "${pageContext.request.contextPath}/backend/ecard/channel/batchCreateChannel/"+channelNum;
			},
			"取 消" : function() {
				$(".validateTips").text("");
				$(this).dialog("close");
				query();
			}
		},
		close : function() {
			$(".validateTips").text("");
			query();
		}
	});
	var preInitPath = $("#formFile").val();
	$("#ImportDataDiv").dialog({
		autoOpen : false,
		height : 220,
		width : 450,
		modal : true,
		buttons : {
			"提 交" : function() {
				var filepath = $("#formFile").val();
				if (preInitPath == filepath) {
			        return false;
			    }
				preInitPath = filepath;
				if(filepath==null || filepath=="") {
					$.dialog.alert({title:"保存",content:"请添加excel文件！"});
					return ;
				}
				
				$(".validateTips").html("<img src='${pageContext.request.contextPath}/images/ajax-loading.gif'/>");
				
				$.ajaxFileUpload
				(
					{
						url:"${pageContext.request.contextPath}/backend/ecard/channel/importChannels", 
						type: 'post',
						secureuri:false,
						fileElementId:'formFile',
						dataType: 'json',
						success: function (data, status)
						{     
							if (data.result==1) {
								$("#ImportDataDiv").dialog("close");
								//$.dialog.alert({title:"保存",content:"导入成功！"});
								//query();
								$.dialog.alert({title:"保存",content:"导入成功！",callback:"query();"});
								$(".validateTips").text("");
							}
							else {
								$.dialog.alert({title:"保存",content:"导入失败 : " + data.errorMessag,callback:"query();"});
								$(".validateTips").text("");
							}
						},
						error: function (data, status, e)
						{
							//$.dialog.alert({title:"保存",content:"导入失败"+e});
							$.dialog.alert({title:"保存",content:"导入失败,请重新导入！",callback:"query();"});
							$(".validateTips").text("");
						}
					}
				);
			},
			"取 消" : function() {
				$(".validateTips").text("");
				$(this).dialog("close");
				query();
			}
		},
		close : function() {
			$(".validateTips").text("");
			$("#formFile").val("").removeClass("ui-state-error");
		}
	});
	$("#editDIV").dialog({
		autoOpen : false,
		bgiframe: true, 
		height : 250,
		width : 400,
		modal : true,
		resizable :false,
       	hide: '',
        show:'',
        zIndex:1000,
		buttons : {
			"保 存" : function() {
				var bValid = doValid();
				if(bValid){
					var isAdd = ($("#dealid").val() == null) || ($.trim($("#dealid").val()) == "")||($.trim($("#dealid").val()) == "0");
					console.log(isAdd);
					var createUrl = "${pageContext.request.contextPath }/backend/ecard/channel/add";
					var updateUrl = "${pageContext.request.contextPath }/backend/ecard/channel/save";
					var URL = isAdd?createUrl:updateUrl;
					$.ajax({
						url:URL,
						type: 'POST',
						data: $("#editFm").serialize(),
						error: function(XMLHttpRequest, textStatus, errorThrown){
						//alert('保存异常。' + textStatus + "/" + errorThrown);
						 $.dialog.alert({title:"保存",content:"保存异常"});
						},
						success: function(data){
							if(data.result==1) {
								$("#createDIV").dialog("close");
								if(isAdd){
									$.dialog.alert({title:"保存",content:"创建成功"});
								}else{
									$.dialog.alert({title:"保存",content:"修改成功"});
								}
								query();
							} else {
								$.dialog.alert({title:"保存",content:"创建失败："+ data.errorMessage});	
							}
						}
					});  
				}
			}, 
			"取 消" : function() {
				$(this).dialog("close");			
			}
		},
		close : function() {
			$(this).dialog("close");
		}
	});
	
	function doValid(){
		var channelCode = $("#dealchannelCode").val();
		if(channelCode == null || $.trim(channelCode) == ''){
			$.dialog.alert({title:"错误",content:"渠道编号不能为空！",objCtrl:"#channelCodeIput"});
			return false;
		}
		var channelName = $("#dealchannelName").val();
		if(channelName == null || $.trim(channelName) == ''){
			$.dialog.alert({title:"错误",content:"渠道名称不能为空！",objCtrl:"#channelNameIput"});
			return false;
		}
		return true;
	}
	
</script>
</body>
</html>